package com.googlecode.gingerman.robot;

import java.util.List;
import ui.Paintable;

import com.googlecode.gingerman.world.World;
import com.googlecode.gingerman.world.geometry.Circle;
import com.googlecode.gingerman.world.geometry.Coordinates;
import com.googlecode.gingerman.world.geometry.Vector;

public class Bullet implements Paintable {
	private double x, y, speed, angle, radius;
	private int stepsLeft;
	private Coordinates coordinates;
	private Circle circle;
	private World world;

	public Bullet(double x, double y, double angle, double speed, int stepsLeft, double radius) {
		this.x = x;
		this.y = y;
		this.angle = angle;
		this.speed = speed;
		this.stepsLeft = stepsLeft;
		this.radius = radius;
		coordinates = new Coordinates(this.x, this.y);
		circle = new Circle(coordinates, this.radius);
	};

	public double getX() {
		return x;
	};

	public double getY() {
		return y;
	};
	
	public double getAngle() {
		return angle;
	};
	
	public double getRadius() {
		return radius;
	};
	
	public double getSpeed() {
		return speed;
	};

	public void setWorld(World world) {
		this.world = world;
	};
	
	public World getWorld() {
		return world;
	};

	public Circle getCircle() {
		return circle;
	};

	public void step() {

		coordinates = new Coordinates(x, y);
		circle = new Circle(coordinates, radius);
		Vector vector = new Vector(coordinates);
		Vector tmp = new Vector(angle, speed);
		vector = vector.add(tmp);
		x = vector.getX();
		y = vector.getY();

		stepsLeft--;
		if (stepsLeft <= 0) {
			world.addBulletTmpDel(this);
			world.getBullets().remove(this);
			return;
		}

		boolean f1 = false;
		coordinates = new Coordinates(x, y);
		this.circle = new Circle(coordinates, radius);
		
		for (int i = 0; i < world.getWalls().size(); i++) {
			f1 = circle.intersectFigure(world.getWalls().get(i).getRectangle());
			if (f1) {
				world.addBulletTmpDel(this);
				world.getBullets().remove(this);
				return;
			}
		}

		for (int i = 0; i < world.getRobots().size(); i++) {
			f1 = circle.intersectFigure(world.getRobots().get(i).getCircle());
			if (f1) {
				world.getRobots().get(i).makeDamage();
				world.addBulletTmpDel(this);
				world.getBullets().remove(this);
				return;
			}
		}
	}

	@Override
	public List<Paintable> getPaintableChildren() {
		return null;
	}

	@Override
	public String getView() {
		return "bullet";
	}

	
}
